<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>补料采购</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="create-order">
      <div>
        <search-btn-view btn-text="条件查询" @before-open="resetSearch" @search="searchData">
          <div slot="search-content">
            <sys-common-search :search="search" request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:6}" label-width="100px"
              input-width="170px" :search-field="searchFields">
            </sys-common-search>
          </div>
        </search-btn-view>
      </div>
      <div v-if="dealerObj!==null">
        <order-item v-for="(order,index) in purchaseOrders" :key="`orderItem_${index}`" :purchase-order="order" style="margin-bottom: 20px;"
          :dealer-list="dealerList" :dealer-obj="dealerObj" @save-success="saveOrderSuccess"></order-item>
      </div>
    </div>
  </div>
</body>

<script src="../../myJs/importFile.js"></script>
<script>
  importFile([
    "/vueSystem/components/css/sysCommonSearch.css",
    "/vueSystem/components/css/searchBtnView.css",
    "./css/orderItem.css"
  ], [
    "/vueSystem/i18n/index.js",
    "/vueSystem/js/dropdownUtil.js",
    "/vueSystem/components/sysCommonSearch.js",
    "/vueSystem/components/searchBtnView.js",
    "/vueSystem/components/tableDealerSelect.js"
  ]);

</script>
<script src="./orderItem.js"></script>
<script>
  var app = new Vue({
    el: '#app',
    data: function () {
      var search = {
        factoryNo: '',
        orderId: '',
        companyOrderId: '',
        customerNo: '',
        materialName: '',
        model: '',
        color: '',
        providerName: ''
      };

      return {
        loading: false,
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        purchaseOrders: [],
        searchFields: ['companyOrderId', 'orderId', 'factoryNo', 'customerNo', 'materialName', 'model', 'color',
          'providerName'
        ],
        dealerList: [],
        dealerObj: null,
        allDealerObj: null
      };
    },
    created: function () {
      this.loading = true;
      var self = this;
      this.setDealerList().then(function () {
        self.setOrderData(true);
      }).catch(function () {
        self.loading = false;
      });
    },
    methods: {
      setDealerList: function () {
        var self = this;
        return DropdownUtil.getDealerList().then(function (res) {
          self.dealerList = res.dealerList;
          self.dealerObj = res.dealerObj;
          self.allDealerObj = res.allDealerObj;
          return res;
        });
      },

      setOrderData: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var postData = {};
        var fields = ['factoryNo', 'orderId', 'companyOrderId', 'customerNo', 'materialName', 'model',
          'color', 'providerName'
        ];
        SysUtil.setPostData(postData, this.search, fields, true);

        this.loading = true;
        var self = this;
        ajaxRequest('order/getPurchaseOrderData', postData).then(function (purchaseOrders) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          if (purchaseOrders.length === 0) {
            self.$alert('没有数据可生成订单采购单', '信息');
          }

          var dealerObj = self.dealerObj;
          for (var i = 0; i < purchaseOrders.length; i++) {
            var purchaseOrder = purchaseOrders[i];
            var providerName = purchaseOrder.providerName;
            var provider;
            if (dealerObj === null) {
              provider = '';
            } else {
              provider = dealerObj[providerName];
              if (SysUtil.isEmpty(provider)) {
                provider = '';
              }
            }
            if (provider === '') {
              provider = {
                id: 0,
                dataId: '0-1',
                name: providerName,
                source: 1,
                isTax: purchaseOrder.isTax === 0
              };
              purchaseOrder.otherProviderOption = _.clone(provider);
            }
            purchaseOrder.provider = provider;
          }
          self.purchaseOrders = purchaseOrders;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      saveOrderSuccess: function () {
        this.setOrderData(true);
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.setOrderData(true);
      }
    },
    components: {
      OrderItem: OrderItem,
      SysCommonSearch: SysCommonSearch,
      SearchBtnView: SearchBtnView
    }
  });

</script>
<style>
  .create-order {
    padding: 20px;
  }

  .el-checkbox__inner {
    border: 1px solid #08c;
  }

</style>

</html>
